
import os
import sys, codecs
from PyQt5.QtCore import *
#from PyQt5.QtCore import Qt
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
def GB2313_to_utf8(fullfilename):  
    fdir,fename=os.path.split(fullfilename)
    newName=fdir+'/utf8-tmp.txt'
    try:
        f = codecs.open(fullfilename,'r+', encoding="gb2312")
        newfile = codecs.open(newName,'w+', encoding="utf-8")
    except IOError:
        f.close() 
    newfile.write(f.read().encode("utf-8").decode( encoding="utf-8" ))
    newfile.flush()
    newfile.close()
    f.close()
    os.remove(fullfilename)     #删除源文件
    #   重命名新文件为旧文件名 os.rename(src , dst)
    try:
        os.rename( newName, fullfilename)
    except(FileNotFoundError):
        print("目录不存在")
def get_hex_info(filename):    
    try:
        with open(filename, 'r') as fin:
            input_file = fin.read()
            input_str = input_file.splitlines(False)  
            max_line = len(input_str) 
            if max_line >=29:
                strlist=input_str[20].split() #   取strlist[3] -- data count
                print(strlist)
                data_count=str(int(strlist[3], 16)-1)
                print(data_count)
                strlist=input_str[27].split() #   取strlist[4]
                xdata_count=str(int(strlist[4], 10))
                print(strlist)
                strlist=input_str[28].split() #   取strlist[3]
                code_count=str(int(strlist[3], 10))
                return data_count, xdata_count, code_count
            pass
    except:
        return None
if __name__ == "__main__":
    #fdir,fename=os.path.split('mcs51/split_function.py')
    filename='readfile/06ADC_8key.mem'
    get_hex_info(filename)
    #    [self.ProjectDir,filename]=os.path.split(self.fullfile)
    filename='F:/prog-tmp/PyQt5-code/MDI_code_compV0.32/code1.txt'
    #GB2313_to_utf8(filename)
    '''

    f.close()
    '''
